package net.xisberto.bustime;

import java.util.Date;

import net.xisberto.bustime.model.Bus;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class PersonalListActivity extends BusListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_personal_list);

		list_id = android.R.id.list;
		
		ListView list = ((ListView)findViewById(list_id));
		list.setOnItemClickListener(this);
		
		try {
			int request_code = getIntent().getIntExtra(BusTimeActivity.REQUEST_CODE, BusTimeActivity.REQUEST_MANAGE);
			switch (request_code) {
			case BusTimeActivity.REQUEST_MANAGE:
				this.title.setText(R.string.title_manage_personal);
				Toast.makeText(getApplicationContext(), R.string.toast_manage_personal, Toast.LENGTH_SHORT).show();
				break;
			case BusTimeActivity.REQUEST_VIEW_RECORD:
			case BusTimeActivity.REQUEST_REGISTER:
				this.title.setText(R.string.title_choose_bus);
				break;
			default:
				this.title.setText(R.string.app_name);
				break;
			}
		} catch (NullPointerException e) {
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		db.openDataBase();
		bus_list = db.listPersonal();
		populateList();
		db.close();
	}

	private void prepareRemove(final int position){
		Bus bus = bus_list.get(position);
		String msg = getApplicationContext().getString(R.string.msg_confirm_delete);
		msg = msg.replace("$1", "\""+bus.getName()+"\"");
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:
					removeItem(position);
					break;
				case DialogInterface.BUTTON_NEGATIVE:
					dialog.cancel();
					break;
				default:
					dialog.cancel();
					break;
				}
			}
		};
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg)
			.setPositiveButton(android.R.string.yes, dialogClickListener)
			.setNegativeButton(android.R.string.no, dialogClickListener)
			.show();
	}
	
	private void removeItem(int position) {
		Bus bus = bus_list.get(position);
		db.openDataBase();
		db.removeFromPersonal(bus.getId());
		bus_list = db.listPersonal();
		db.close();
		Log.i(getApplicationContext().getPackageName(),"Items na lista: "+bus_list.size());
		populateList();
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
		int request_code = getIntent().getIntExtra(BusTimeActivity.REQUEST_CODE, BusTimeActivity.REQUEST_MANAGE);
		Log.i(getApplicationContext().getPackageName(), "Request code: "+request_code);
		Bus bus = null;
		switch (request_code) {
		case BusTimeActivity.REQUEST_MANAGE:
			prepareRemove(position);
			break;
		case BusTimeActivity.REQUEST_VIEW_RECORD:
			bus = bus_list.get(position);
			getIntent().putExtra(BusTimeActivity.BUS_ID, bus.getId());
			Log.i(getApplicationContext().getPackageName(), "Id selecionado: "+bus.getId());
			this.setResult(RESULT_OK, getIntent());
			this.finish();
			break;
		case BusTimeActivity.REQUEST_REGISTER:
			db.openDataBase();
			bus = bus_list.get(position);
			long time = getIntent().getLongExtra(BusTimeActivity.RECORD_TIME, new Date().getTime());
			double latitude = getIntent().getDoubleExtra(BusTimeActivity.RECORD_LATITUDE, 0);
			double longitude = getIntent().getDoubleExtra(BusTimeActivity.RECORD_LONGITUDE, 0);
			long insert = db.registerBus(time, latitude, longitude, bus.getId());
			db.close();
			if (insert != -1) {
				this.setResult(RESULT_OK);
			} else {
				this.setResult(RESULT_CANCELED);
			}
			this.finish();
		default:
			break;
		}
	}

}
